import { Point, Polygon } from "ol/geom";
import { Circle as CircleStyle,Icon, Fill, Stroke, Style } from "ol/style";
import GCJ284 from "@/assets/js/map/GCJ284";
import { addProjection, addCoordinateTransforms, transform } from 'ol/proj.js';
import Feature from 'ol/Feature';
export default {
  toGeojson(points, type) {
    var featureCollection = {
      "type": "FeatureCollection",
      "features": []
    }
    for (let i = 0; i < points.length; i++) {
      const region = points[i].region.split(",");
      var feature = {
        "type": "Feature",
        "properties": {},
        "geometry": {
          "type": type,
          "coordinates": [
            []
          ]
        }
      }
      for (let j = 0; j < region.length; j++) {
        const point = region[j].split(" ");
        feature.geometry.coordinates[0].push([Number(point[0]), Number(point[1])]);
      }
      featureCollection.features.push(feature)
    }
    return featureCollection;

  },

  GDToGeojson(points, type) {
    var line = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": []
      }
    }
    var start = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": []
      }
    }

    var end = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": []
      }
    }
    var featureCollection = {
      "type": "FeatureCollection",
      "features": [start, line, end]
    }
    for (var i = 0; i < points.length; i++) {
      if (isNaN(points[i][0])) {
        continue;
      }
      var obj = GCJ284.transformGCJ2WGS(points[i][1], points[i][0]);
      var point = transform([obj.lon, obj.lat], 'EPSG:4326', 'EPSG:3857');
      if (i == 0) {
        start.geometry.coordinates = point;
      } else if (i == points.length - 1) {
        end.geometry.coordinates = point;
      }
      line.geometry.coordinates.push(point);
    }
    return featureCollection;
  },
  getStyle(feature) {
    var image = new CircleStyle({
      radius: 5,
      fill: null,
      stroke: new Stroke({ color: "red", width: 1 })
    });

    var styles = {
      Point: new Style({
        image: image
      }),
      LineString: new Style({
        stroke: new Stroke({
          color: "blue",
          width: 3
        })
      }),
      MultiLineString: new Style({
        stroke: new Stroke({
          color: "green",
          width: 1
        })
      }),
      MultiPoint: new Style({
        image: image
      }),
      MultiPolygon: new Style({
        stroke: new Stroke({
          color: "yellow",
          width: 1
        }),
        fill: new Fill({
          color: "rgba(255, 255, 0, 0.1)"
        })
      }),
      Polygon: new Style({
        stroke: new Stroke({
          color: "blue",
          lineDash: [4],
          width: 3
        }),
        // fill: new Fill({
        //   color: "rgba(0, 0, 255, 0.1)"
        // })
      }),
      GeometryCollection: new Style({
        stroke: new Stroke({
          color: "magenta",
          width: 2
        }),
        fill: new Fill({
          color: "magenta"
        }),
        image: new CircleStyle({
          radius: 10,
          fill: null,
          stroke: new Stroke({
            color: "magenta"
          })
        })
      }),
      Circle: new Style({
        stroke: new Stroke({
          color: "red",
          width: 2
        }),
        fill: new Fill({
          color: "rgba(255,0,0,0.2)"
        })
      })
    };
    return styles[feature.getGeometry().getType()];
  },
  hideLayer(map, layerId) {
    // var map = maps[mapId];
    var layers = map.getLayers().array_
    layers.forEach(function (layer) {
      let id = layer.values_.id
      if (layerId == id) {
        layer.setVisible(false);
        return;
      }
    })
  },
  showLayer(mapId, layerId) {
    var map = maps[mapId];
    var layers = map.getLayers().array_
    layers.forEach(function (layer) {
      let id = layer.values_.id
      if (layerId == id) {
        layer.setVisible(true)
        return;
      }
    })
  },
  //地图缩放，通过extent范围  extent数据类型为int数组，不能为字符串，如果为字符串则先转换为int数组。
  zoomToExtent(map,extent) {
    var MapZoom = extent;
    var mapPadding = [20, 10, 20, 10];
    //  alert(isArrayFn(extent));
    if (!Array.isArray(extent)) {
      var MapZoomString = $(this).attr("zoom").split(",");
      MapZoom = MapZoomString.map(function (data) {
        return +data;
      });
    } else {
      mapPadding = [120, 110, 120, 410];
    }
    //添加map.getSize()，可以让地图在正常的缩放级别内，而不是出现地图级别小数。
    map.getView().fit(MapZoom, map.getSize(), {
      constrainResolution: false,
      earest: false,
      padding: mapPadding
    });
    // 以下为另外一种方法
    //var r = map.getView().getResolutionForExtent(extent, map.getSize());
    //map.getView().setResolution(r);
    //map.getView().setCenter(ol.extent.getCenter(extent));
  },
  createIconStyle(url){
    return new Style({
        image: new Icon({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        src: url
     })
    });
  },
  createPoint(x,y,flag){
    if(!flag){
      var point = transform([x, y], 'EPSG:4326', 'EPSG:3857');
    }else {
      point = [x,y];
    }
    return new Feature({
      geometry: new Point(point),
      population: 4000,
      rainfall: 500
    });
  }

}
